package com.june.pubsub;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Publisher {

	private static final String TOPIC_NAME = "testTopic";

	public static void main(String[] args) throws JMSException {
		
		// 创建一个连接工厂 TopicConnectionFactory
		// user/password/url: admin/admin/tcp://localhost:61616
		TopicConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnectionFactory.DEFAULT_USER, 
				ActiveMQConnectionFactory.DEFAULT_PASSWORD, 
				ActiveMQConnectionFactory.DEFAULT_BROKER_URL);
		// 从连接工厂得到一个连接 TopicConnection
		TopicConnection connection = connectionFactory.createTopicConnection();
		// 通过连接来建立一个会话
		TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
		Topic topic = session.createTopic(TOPIC_NAME);
		TopicPublisher publisher = session.createPublisher(topic);
		for (int i = 0; i < 10; i++) {			
			TextMessage message = session.createTextMessage("Hello testTopic JMS text" + i);
			publisher.publish(message);
		}
		System.out.println("消息主题：" + TOPIC_NAME + "发布成功");

		publisher.close();
		session.close();
		connection.close();
	} 

}
